import {
	TOKENNAME,
	USER_INFO,
	SHOP_INFO,
	USER_TYPE,
	USER_SIGN_STATUS,
	AGREEMENT_NAME,
	GUIDE_NAME
} from '@/config.js'
import {
	USER_LOCAL_ADDRESS,
	USER_SHOP_CART_ADDRESS
} from '@/storage.js'
import dayjs from 'dayjs'
// 初始化基本信息
async function base({
	dispatch,
	commit
}) {
	// 初始化banner
	dispatch('initBanner')
	// 用户-初始化公告列表信息
	dispatch('initNotice')
	// 初始化分类列表
	dispatch('initCategory', 1)
	// 初始化分类列表
	dispatch('initCategory', 2)
	// 初始化客服电话
	dispatch('initContactPhone')
}

async function initUser({
	dispatch,
	commit
}) {
	// 获取个人信息
	let userInfo = uni.getStorageSync(USER_INFO)
	let shopInfo = uni.getStorageSync(SHOP_INFO)
	
	// 请求接口根据用户类型进行判断路由显示
	let accountType = uni.getStorageSync(USER_TYPE)
	setTimeout(() => {
		commit('UPDATE_TABBAR_VISIBLE_TYPE', accountType)
	}, 100)
	
	if (!uni.getStorageSync(AGREEMENT_NAME)) {
		uni.reLaunch({ url: '/pages/once/start/start' })
	} else if (!uni.getStorageSync(GUIDE_NAME)) {
		uni.reLaunch({ url: '/pages/once/guide/guide' })
	} else if (userInfo || shopInfo){
		await dispatch('initAdvert')
		
		if (userInfo) {
			userInfo = JSON.parse(userInfo)
			commit('UPDATE_USER_INFO', userInfo)
			
			let localAddress = uni.getStorageSync(USER_LOCAL_ADDRESS)
			commit('UPDATE_USER_LOCAL_ADDRESS', localAddress && JSON.parse(localAddress) || {})
			
			let shopcartAddress = uni.getStorageSync(USER_SHOP_CART_ADDRESS)
			commit('UPDATE_USER_SHOP_CART_ADDRESS', shopcartAddress && JSON.parse(shopcartAddress) || {})
			
			// 初始化
			dispatch('initUserData')
		}
		
		if (shopInfo) {
			shopInfo = JSON.parse(shopInfo)
			commit('UPDATE_SHOP_INFO', shopInfo)
		}
		commit('BEFORE_INTO_INDEX', 'blank')
	} else {
		commit('BEFORE_INTO_INDEX', 'blank')
	}
}

const initConfig = (store) => {
	// console.log(plus.os.name == "iOS")
	// if (plus.os.name == "iOS") {
	// 	uni.removeStorageSync(AGREEMENT_NAME)
	// 	uni.removeStorageSync(GUIDE_NAME)
	// }
	initUser(store)
	
	base(store);
}

export default initConfig
